package com.huang.lesson03;

import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

public class TestClient {
    public static void main(String[] args) {
        Socket socket =null;
        OutputStream os =null;
        InputStream is =null;
        InputStream inputStream =null;
        ByteArrayOutputStream stream =null;
        try {
            InetAddress address = InetAddress.getByName("127.0.0.1");
            int port=90;
             socket = new Socket(address, port);
             os = socket.getOutputStream();
             //传输文件  先读成流对象  在写出去
             is = new FileInputStream(new File("C:/Users/HJW/Pictures/h.jpg"));
            byte[] bytes = new byte[1024];
            int len;
            while ((len=is.read(bytes))!=-1){
                os.write(len);
            }
            //我传输完了
            socket.shutdownOutput();


            //客户端传来  消息告诉我他已经接受
             inputStream = socket.getInputStream();
             stream = new ByteArrayOutputStream();
            byte[] bytes2 = new byte[1024];
            int len2;
            while ((len2=inputStream.read(bytes2))!=-1){
                stream.write(bytes2,0,len2);
            }
            System.out.println(stream.toString());

        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                stream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                os.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}
